<template>
  <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="pass-ruleForm">
    <el-form-item label="旧密码" prop="oldPassWord">
      <el-input type="password" v-model="ruleForm2.oldPassWord" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="newPassWord">
      <el-input type="password" v-model="ruleForm2.newPassWord" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm2')">确认</el-button>
      <!-- <el-button @click="resetForm('ruleForm2')">重置</el-button> -->
    </el-form-item>
  </el-form>
</template>

<script>
  import {modifyPassWord} from '@/server/personCenter'

  export default {
    data() {
      var validateOldPass = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入旧密码'));
        }else{
          callback();
        }

      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm2.newPassWord) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm2: {
          newPassWord: '',
          checkPass: '',
          oldPassWord: ''
        },
        rules2: {
          newPassWord: [
            { required: true, validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { required: true, validator: validatePass2, trigger: 'blur' }
          ],
          oldPassWord: [
            { required: true, validator: validateOldPass, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.modifyPassWordFn(this.ruleForm2);
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      modifyPassWordFn(paramObj) {
        modifyPassWord(paramObj).then((res) => {

          if (res.code == '1') {

            this.$notify({
              // title: '恭喜你',
              message: res.message,
              type: 'success',
              duration: 3000
            });

            this.ruleForm2 = {
              newPassWord: '',
              checkPass: '',
              oldPassWord: ''
            };

          } else {
           this.$message.error(res.message);
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
